﻿using System;
using System.Threading;
using System.ComponentModel;
using System.Diagnostics;
namespace OtherDevice
{
    [Description("奥豪斯电子天平")]
    public class OHAUS_Balance: Balance_Base
    {
        public OHAUS_Balance(string strName) : base(strName)
        {
            controlFrom = new Form_称重();
        }

        Object muxt = new Object();
        public override bool Get(int n_取值次数, int n_间隔时间, ref double pos)
        {
            try
            {
                lock (muxt)
                {
                    double d_Sum = 0;
                    for (int i = 0; i < n_取值次数; i++)
                    {
                        if (i != 0)
                            System.Threading.Thread.Sleep(n_间隔时间);
                        var ReadCMD = "P\r\n";
                        byte[] byteRe = null;
                        if (SendAndReciveData(ReadCMD, ref byteRe, 500))
                        {
                            string str = System.Text.Encoding.Default.GetString(byteRe);
                            string[] strs = str.Split('g');
                            if (strs[0] == "Over load")
                            {
                                pos = -99999;
                                return false;
                            }
                            d_Sum += double.Parse(strs[0]);
                        }
                        else
                        {
                            return false;
                        }
                    }
                    pos = d_Sum / n_取值次数;
                    return true;
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"称重值获取失败{ex}");
                return false;
            }
        }
        public override void SetZore()
        {
            var ReadCMD = "Z\r\n";
            byte[] byteRe = new byte[] { };
            SendAndReciveData(ReadCMD, ref byteRe, 500);
            return;

        }
    }
}
